package alipayex

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-pay/gopay"
	"github.com/gogap/logs"
)

// alipay.open.iotmbs.isvhotel.create(iot酒店行业isv创建酒店)
//	文档地址：https://opendocs.alipay.com/pre-apis/06xw30
func (a *Client) IotmbsIsvhotelCreate(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotmbsIsvhotelCreateRsp, err error) {
	err = bm.CheckEmptyError("project_name", "project_flag", "appid", "shop_id")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.open.iotmbs.isvhotel.create"); err != nil {
		return nil, err
	}
	aliRsp = new(IotmbsIsvhotelCreateRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

// alipay.open.iotmbs.isvdevice.bind(isv绑定酒店设备)
//	文档地址：https://opendocs.alipay.com/pre-apis/06xku4
func (a *Client) IotmbsIsvdeviceBind(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotmbsIsvdevicebindRsp, err error) {
	err = bm.CheckEmptyError("sn", "project_id", "device_category_code", "scene_category_code", "device_solution")
	if err != nil {
		return nil, err
	}

	var url []byte
	if url, err = a.doAliPay(ctx, bm, "alipay.open.iotmbs.isvdevice.bind"); err != nil {
		return nil, err
	}
	logs.Info("param======>", string(url))
	bs, err := a.httpGet(string(url))
	if err != nil {
		return nil, err
	}

	aliRsp = new(IotmbsIsvdevicebindRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

// alipay.open.iotmbs.isvdevice.unbind(isv解绑设备)
//	文档地址：https://opendocs.alipay.com/pre-apis/06xy8o
func (a *Client) IotmbsIsvdeviceUnbind(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotmbsIsvdeviceUnbindRsp, err error) {
	err = bm.CheckEmptyError("sn", "project_id")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.open.iotmbs.isvdevice.unbind"); err != nil {
		return nil, err
	}
	aliRsp = new(IotmbsIsvdeviceUnbindRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

// alipay.open.iotmbs.image.upload(图片上传)
// 文档地址：https://opendocs.alipay.com/pre-apis/07tmh4
func (a *Client) IotmbsImageUpload(ctx context.Context, bm gopay.BodyMap, fileName string) (aliRsp *IotmbsImageUploadRsp, err error) {
	err = bm.CheckEmptyError("biz_code")
	if err != nil {
		return nil, err
	}

	var bs []byte
	if bs, err = a.doAliPayParam(ctx, bm, "alipay.open.iotmbs.image.upload", fileName); err != nil {
		return nil, err
	}

	aliRsp = new(IotmbsImageUploadRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

// alipay.open.iotmbs.userpermission.identify(iot用户权限识别)
// 文档地址：https://opendocs.alipay.com/pre-apis/07ym2c
func (a *Client) IotmbsUserpermisionIdentify(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotmbsUserpermissionIdentifyRsp, err error) {
	err = bm.CheckEmptyError("param_type", "param_id", "project_id", "sn")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.open.iotmbs.userpermission.identify"); err != nil {
		return nil, err
	}
	aliRsp = new(IotmbsUserpermissionIdentifyRsp)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}
